<?php
/*
 * @Date: 2021-02-03 22:10:31
 * @名称: comics_add_invitation_aact 操作控制器
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-02-28 22:19:21
 * @FilePath: /Site/app/useradmin/controller/DataBaseController/ComicsAddInvitationAact.php
 */

namespace app\useradmin\controller\DataBaseController;

use app\BaseController;
use think\facade\Db;

class ComicsAddInvitationAact extends BaseController
{
    /**
     * 查询用户权限
     *
     * @param String $UserId //用户id
     * @param String $BookId //书籍id
     * @param List $Permission //要查询的权限默认
     * @return Json
     */
    public function Select($UserId, $BookId, $Permission = ['add_time', "private_key", 'certification_time'])
    {
        $return_data = Db::table('comics_add_invitation_aact')
            ->where([
                "comics_id" => $BookId,
                "user_id" => $UserId,
            ])
            ->field($Permission)
            ->order("id desc")
            ->select();
        $return_data = successJsonReturn($return_data);
        $return_data = json_decode($return_data->getContent(), true);
        return $return_data;
    }
    /**
     * 查询用户权限 - 只查询最后一条 请注意 只会查询 注册码过期的时间段内的
     *
     * @param String $UserId //用户id
     * @param String $BookId //书籍id
     * @param List $Permission //要查询的权限默认 
     * @return Json
     */
    public function find($UserId, $BookId, $Permission = ['add_time', "private_key", 'certification_time'])
    {
        $codeLostTime = date("Y-m-d h:i:s", time() - config("useradminConfig.ValidTimeOfActivationFile"));
        $return_data = Db::table('comics_add_invitation_aact')
            ->where([
                "comics_id" => $BookId,
                "user_id" => $UserId,
            ])
            ->field($Permission)
            ->whereTime('add_time', '>', $codeLostTime)
            ->order("id desc")
            ->find();
        $return_data = successJsonReturn($return_data);
        $return_data = json_decode($return_data->getContent(), true);
        return $return_data;
    }
    /**
     * @方法名: 插入数据库
     * @作者: 初雪桜
     * @信息: 插入到 comics_add_invitation_aact
     * @param String $UserId 用户id
     * @param String $BookId 书籍id
     * @param Datetime $add_time 添加时间
     * @param String $private_key 私钥
     * @param String $operator_user_id 添加人id
     * @param Datetime $certification_time 认证时间
     * @param String $aact_id author_and_chinese_team 表中的 _id
     * @return {*}
     */
    public function Insert($UserId, $BookId, $add_time, $private_key, $operator_user_id, $certification_time = null, $aact_id)
    {
        $return_data = Db::table('comics_add_invitation_aact')
            ->insertGetId(
                [
                    "_id" => randIdStructure(7),
                    "user_id" => $UserId,
                    "comics_id" => $BookId,
                    "add_time" => $add_time,
                    "private_key" => $private_key,
                    "certification_time" => $certification_time,
                    "operator_user_id" => $operator_user_id,
                    "aact_id" => $aact_id,
                ]
            );
        if ($return_data) {
            return json_decode(successJsonReturn($return_data)->getContent(), true);
        } else {
            return json_decode(errorJsonReturn()->getContent(), true);
        }
    }

    /**
     * @方法名: 更新数据库 
     * @作者: 初雪桜
     * @信息: 对数据库comics_add_invitation_aact 进行更新操作
     * @param String $id 数据id
     * @param String $BookId 书籍id
     * @param String $UserId 用户id
     * @param String $private_key 密钥
     * @param Json $Permission 更新的数据
     * @return Json
     */
    public function update($id, $BookId, $UserId, $private_key, $Permission)
    {
        $return_data = Db::table('comics_add_invitation_aact')
            ->where([
                "id" => $id,
                "comics_id" => $BookId,
                "user_id" => $UserId,
                "private_key" => $private_key,
            ])
            ->update($Permission);
        if ($return_data) { //更新数据成功
            $return_data = successJsonReturn($return_data);
            $return_data = json_decode($return_data->getContent(), true);
            return $return_data;
        } else {
            return json_decode(errorJsonReturn()->getContent(), true);
        }
    }
}
